package com.yueshop.member.controller;

import java.util.Arrays;
import java.util.Map;


import com.yueshop.common.exception.BizCodeEnum;
import com.yueshop.member.exception.PhoneExistException;
import com.yueshop.member.exception.UsernameExistException;
import com.yueshop.member.feign.CouponFeignService;
import com.yueshop.member.vo.MemberRegistVo;
import com.yueshop.member.vo.SocialUserVo;
import com.yueshop.member.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yueshop.member.entity.MemberEntity;
import com.yueshop.member.service.MemberService;
import com.yueshop.common.utils.PageUtils;
import com.yueshop.common.utils.R;



/**
 * 会员
 *
 * @author cr
 * @email cr@gmail.com
 * @date 2021-04-09 14:41:45
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;



    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUserVo vo){

      MemberEntity memberEntity=  memberService.login(vo);
      if(memberEntity!=null){
          return  R.ok().setData(memberEntity);
      }else{
          return null;
      }
    }



    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo vo){
        MemberEntity memberEntity=  memberService.login(vo);
        if(memberEntity ==null){
            return R.error(BizCodeEnum.LOGINACCT_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_INVALID_EXCEPTION.getMsg());
        }else{
            return R.ok().setData(memberEntity);
        }
    }




    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){
        try {
            memberService.regist(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UsernameExistException e) {
            return R.error(BizCodeEnum.USERNAME_EXIST_EXCEPTION.getCode(),BizCodeEnum.USERNAME_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("coupons")
    public R test1(){
        MemberEntity entity = new MemberEntity();
        entity.setUsername("张三");

        R r = couponFeignService.memberList();

        return  R.ok().put("member",entity).put("coupons",r.get("coupons"));
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
